package com.wzzc.service;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.xmlbeans.impl.tool.FactorImports;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wzzc.bean.Student;
import com.wzzc.bean.StudentExample;
import com.wzzc.bean.StudentExample.Criteria;
import com.wzzc.dao.StudentMapper;
import com.wzzc.utils.DockerUtils;
import com.wzzc.utils.MyUtils;

@Service
public class UserService {
	
	@Autowired
	private StudentMapper studentMapper;
	
	/**
	 * 用户登录
	 */
	public Student login(String username,String password) {
		StudentExample example=new StudentExample();
		Criteria criteria = example.createCriteria();
		criteria.andStudentNoEqualTo(username);
		criteria.andStudentPasswordEqualTo(password);
		List<Student> list = studentMapper.selectByExample(example);
		if (list==null||list.size()==0) {
			return null;
		}
		return list.get(0);
	}
	
	/**
	 * 从Excel导入用户数据
	 */
	public Map<String,List<Integer>> readExcelToDB (File file) {
		Map<String,List<Integer>> map=new HashMap<String, List<Integer>>();
		if (!file.exists()) {return null;}
			List<Integer> successList=new ArrayList<Integer>();
			List<Integer> failList=new ArrayList<Integer>();
		try {
			Workbook workbook = WorkbookFactory.create(new FileInputStream(file));
			Sheet sheet = workbook.getSheetAt(0);
			int numberOfRows = sheet.getPhysicalNumberOfRows();
			for (int i = 1; i < numberOfRows; i++) {
				try {
					Row row = sheet.getRow(i);
					String studentNo=((int)row.getCell(0).getNumericCellValue())+"";
					String studentPassword="";
					switch (row.getCell(1).getCellType()) {
					case Cell.CELL_TYPE_NUMERIC:
						studentPassword=((int)row.getCell(1).getNumericCellValue())+"";
						break;
					case Cell.CELL_TYPE_STRING:
						studentPassword=row.getCell(1).getStringCellValue();
						break;
					}
					String studentRealname=row.getCell(2).getStringCellValue();
					String studentClassname=row.getCell(3).getStringCellValue();

					Student student=new Student(studentNo, studentPassword, studentRealname, studentClassname);
					studentMapper.insertIfUnExistsByNo(student);
					successList.add(i);
				} catch (Exception e) {
					e.printStackTrace();
					failList.add(i);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		map.put("successList", successList);
		map.put("failList", failList);
		return map;
	}
	
	/**
	 * 查询全部班级名称
	 */
	public List<String> getAllClassName() {
		return studentMapper.selectAllClass();
	}
	
	/**
	 * 查询班级下的所有学生
	 */
	public List<Student> selectStudenByClassname(String classname) {
		StudentExample example=new StudentExample();
		Criteria criteria = example.createCriteria();
		criteria.andStudentClassnameEqualTo(classname);
		List<Student> list = studentMapper.selectByExample(example);
		return list;
	}
	
	/**
	 * 用户添加DockerUIID
	 */
	public void updateStudentUIID(Integer id,String uiid) {
		Student student=new Student();
		student.setDockerUuid(uiid);
		StudentExample example=new StudentExample();
		Criteria criteria = example.createCriteria();
		criteria.andIdEqualTo(id);
		studentMapper.updateByExampleSelective(student, example);
	}
	
	/**
	 * 更新用户Docker镜像状态
	 */
	public void updateStudentDockerState(String uiid,Student student) {
		StudentExample example=new StudentExample();
		Criteria criteria = example.createCriteria();
		criteria.andDockerUuidEqualTo(uiid);
		studentMapper.updateByExampleSelective(student, example);
	}
	
	/**
	 * 关闭当前班级Docker镜像
	 */
	public boolean updateDockerStateByClassname(String className) {
		List<String> nameList = studentMapper.selectNameByClassname(className);
		try {
			for (int i = 0; i < nameList.size(); i++) {
				DockerUtils.closeDocker(nameList.get(i));
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		Student student=new Student();
		student.setDockerState(0);
		StudentExample example=new StudentExample();
		Criteria criteria = example.createCriteria();
		criteria.andDockerStateEqualTo(1);
		criteria.andStudentClassnameEqualTo(className);
		studentMapper.updateByExampleSelective(student, example);
		return true;
	}
	
	/**
	 * 关闭所有Docker镜像
	 */
	public void updateDockerStateCloseALL() {
		Student student=new Student();
		student.setDockerState(0);
		StudentExample example=new StudentExample();
		Criteria criteria = example.createCriteria();
		criteria.andDockerStateEqualTo(1);
		studentMapper.updateByExampleSelective(student, example);
	}
}
